/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hz.cheetah.config.spring;

import com.hz.cheetah.client.CheetahByJDKProxy;
import com.hz.cheetah.client.ServiceBeanFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.StringUtils;

/**
 * ReferenceFactoryBean
 * 
 * @author chuanyang
 * @export
 */
public class ReferenceBean<T>  implements FactoryBean, ApplicationContextAware, InitializingBean, DisposableBean {

	private static final long serialVersionUID = 213195494150089726L;
	
	private transient ApplicationContext applicationContext;

    private String factory;

    private Class<?>             interfaceClass;

    private String               version;

	public ReferenceBean() {
        super();
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

    public void setFactory(String factory) {
        this.factory = factory;
    }

    @Override
    public void destroy() throws Exception {
        interfaceClass = null;
        version = null;
    }

    @Override
    public Object getObject() throws Exception {
        ServiceBeanFactory rpcProxy = (ServiceBeanFactory)applicationContext.getBean(this.factory);
        if(StringUtils.isEmpty(version)){
            version = "";
        }
        return rpcProxy.create(interfaceClass,version);
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
            getObject();
    }



    public Class<?> getInterfaceClass() {
        return interfaceClass;
    }

    public void setInterfaceClass(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}